<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
    $(function() {
        var date = new Date(1970,1,1);
        $("a").click(function () {
            var now = new Date();
            var time = (now.getTime() - date.getTime())/1000/60;
            if(time < 1){
                alert("两次获取验证码间隔不得小于一分钟，请稍后再试！");
                return;
            }else {
                var email = $("#email").val();
                $.ajax({
                    type: "GET",
                    url: "http://www.test.com/api/user/isRegistered" + "/" + email,                  
                    success: function (data) {
                        if (data == true) {
                            alert("该邮箱已被注册！");
                        } else {
                            $.ajax({
                                type: "GET",
                                url: "http://www.test.com/api/code/create" + "/" + email,
                                success: function (data) {
                                    if (data == false) {
                                        alert("获取失败，请联系管理员。");
                                    } else {
                                        date = new Date();
                                        alert("验证码已发送至您的注册邮箱！");
                                    }
                                }
                            });
                        }
                    }
                });
            }
        });

        $("#register").click(function () {
            var email = $("#email").val();
            var code = $("#code").val();
            var password = $("#password").val();
            var confirm = $("#confirm").val();
            if(password!=confirm){
                alert("密码输入不一致！");
                return;
            }else {
                $.ajax({
                    type: "GET",
                    url: "http://www.test.com/api/code/validate" + "/" + email + "/" + code,
                    success: function (data) {
                        if (data == 0) {
                            $.ajax({
                                type: "POST",
                                url: "http://www.test.com/api/user/register"+ "/" + email + "/" + password + "/" + code,
                                success: function (data) {
                                    if (data == true) {
                                        alert("注册成功！");
                                        location.href = "login.html";
                                    } else {
                                        alert("注册失败，请联系管理员。");
                                    }
                                }
                            });
                        } else if (data == 1) {
                            alert("输入的验证码不正确！");
                        } else if (data == 2) {
                            alert("该验证码已失效，请重新发送！");
                        } else {
                            alert("请先获取验证码！");
                        }
                    }
                });
            }
        });
    });
</script>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>注册</title>
</head>
<body>
<center>
    邮箱&nbsp;<input id="email" type="text"/><br>
    密码&nbsp;<input id="password" type="password"/><br>
    确认密码&nbsp;<input id="confirm" type="password"/><br>
    验证码&nbsp;<input id="code" type="text"/>&nbsp;<a href="#">获取验证码</a><br>
    <input id="register" type="button" style="background-color: #339933;width: 180px;color: #FFFFFF" value="注册"><br>
</center>
</body>
</html>